package com.dtwenty.gaming.client.widgets;

import com.dtwenty.gaming.data.PlayerCharacter;
import com.dtwenty.gaming.data.PlayerCharacterFieldChangeEvent;
import com.dtwenty.gaming.data.PlayerCharacterFieldChangeHandler;

public class DefensesPanel extends CharacterSheetBox {

	private PlayerCharacter character = null;
	
	private DefenseFields armorClass = new DefenseFields();
	private DefenseFields fortitutde = new DefenseFields();
	private DefenseFields reflex = new DefenseFields();
	private DefenseFields will = new DefenseFields();
	
	DefensesPanel(PlayerCharacter character) {
		this.character = character;
		
		setup();
	}
	
	private void setup() {
		setHeaderText("DEFENSES");
		
		character.addPlayerCharacterFieldChangeHandler(new PlayerCharacterFieldChangeHandler() {
			public void onPlayerCharacterFieldChange(PlayerCharacterFieldChangeEvent event) {
				handleCharacterChange(event);
			}
		});
	}
	
	private void handleCharacterChange(PlayerCharacterFieldChangeEvent change) {
		switch (change.getFieldName()) {
//		case LEVEL:
////			halfLvl = PlayerCharacter.calculateHalfLevel(character.getLevel());
////			halfLvlLabel.setText(halfLvl.toString());
////			scoreLabel.setText(generateScore().toString());
//			break;
		case STRENGTH:
			//need to change the other to conform to this as the modifiers are important
			if(
					PlayerCharacter.calculateAbilityModifier((Integer)change.getValue()) > 
					PlayerCharacter.calculateAbilityModifier(character.getConstitution())
			) fortitutde.setAbil((Integer)change.getValue());
			break;
		case CONSTITUTION:
			if(((Integer)change.getValue()) > character.getStrength()) fortitutde.setAbil((Integer)change.getValue());
			break;
		case DEXTERITY:
//			dex = PlayerCharacter.calculateAbilityModifier(character.getDexterity());
//			dexLabel.setText(dex.toString());
//			scoreLabel.setText(generateScore().toString());
			armorClass.setAbil((Integer)change.getValue());
			if(((Integer)change.getValue()) > character.getIntelligence()) reflex.setAbil((Integer)change.getValue());
			break;
		case INTELLIGENCE:
			if(((Integer)change.getValue()) > character.getDexterity()) reflex.setAbil((Integer)change.getValue());
			break;
		case WISDOM:
			if(((Integer)change.getValue()) > character.getCharisma()) will.setAbil((Integer)change.getValue());
			break;
		case CHARISMA:
			if(((Integer)change.getValue()) > character.getWisdom()) will.setAbil((Integer)change.getValue());
			break;
		case FIELD_MODIFIER:
//			calculateMisc();
//			miscLabel.setText(misc.toString());
//			scoreLabel.setText(generateScore().toString());
			break;
		}
	}
	private class DefenseFields {
		private Integer abil = 0;
		private Integer characterClass = 0;
		private Integer feat = 0;
		private Integer ENH = 0;
		private Integer misc1 = 0;
		private Integer misc2 = 0;
		
		public DefenseFields() {}

		public Integer getTenPlusHalfLevel() {
			return 10 + PlayerCharacter.calculateHalfLevel(character.getLevel());
		}

		public Integer getAbil() {
			return abil;
		}

		public void setAbil(Integer abil) {
			this.abil = abil;
		}

		public Integer getCharacterClass() {
			return characterClass;
		}

		public void setCharacterClass(Integer characterClass) {
			this.characterClass = characterClass;
		}

		public Integer getFeat() {
			return feat;
		}

		public void setFeat(Integer feat) {
			this.feat = feat;
		}

		public Integer getENH() {
			return ENH;
		}

		public void setENH(Integer ENH) {
			this.ENH = ENH;
		}

		public Integer getMisc1() {
			return misc1;
		}

		public void setMisc1(Integer misc1) {
			this.misc1 = misc1;
		}

		public Integer getMisc2() {
			return misc2;
		}

		public void setMisc2(Integer misc2) {
			this.misc2 = misc2;
		}
		
		public Integer getScore() {
			Integer ret = 0;
			ret += getTenPlusHalfLevel();
			ret += getAbil();
			ret += getCharacterClass();
			ret += getFeat();
			ret += getENH();
			ret += getMisc1();
			ret += getMisc2();
			return ret;
		}
	}
}
